package controller.log;

import base.response.OffsetLimitPage;
import controller.common.BaseController;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import services.log.TableEditLogServices;
import util.DateUtil;
import wt.entity.log.TableEditLogExample;

import java.util.Date;

/**
 * 功能:
 * 作者: 洪世成
 * 日期: 2017/9/9
 * 版权所有: admin
 */
@Controller
@RequestMapping("log/tableEditLog")
public class TableEditLogController extends BaseController {
    @Autowired
    TableEditLogServices tableEditLogServices;


    @RequiresPermissions("log:tableEditLog:default")
    @RequestMapping("index")

    public String index() {
        return "log/tableEditLog/index";
    }




    @RequiresPermissions("log:tableEditLog:default")
    @ResponseBody
    @RequestMapping("select")
    public OffsetLimitPage select(
            @DateTimeFormat(pattern = "yyyy-MM-dd") Date startTime,
            @DateTimeFormat(pattern = "yyyy-MM-dd") Date endTime,
            String tableName,
            String accountId,
            String ip,
            Short crudType,
            Integer offset,
            Integer limit
    ) throws Exception {
        TableEditLogExample tableEditLogExample = new TableEditLogExample();
        tableEditLogExample.setOrderByClause("id desc" );
        TableEditLogExample.Criteria criteria = tableEditLogExample.createCriteria();
        if (startTime != null) {
            criteria.andRequestTimeGreaterThanOrEqualTo(startTime);
        }
        if (endTime != null) {
            criteria.andRequestTimeLessThan(DateUtil.moveDate(endTime,1));
        }
        if (!StringUtils.isBlank(tableName)) {
            criteria.andTableNameEqualTo(tableName);
        }
        if (!StringUtils.isBlank(accountId)) {
            criteria.andAccountIdEqualTo(accountId);
        }
        if (!StringUtils.isBlank(ip)) {
            criteria.andIpEqualTo(ip);
        }
        if (crudType != null) {
            criteria.andTableOperationEqualTo(crudType);
        }
        return tableEditLogServices.selectByExampleWithBLOBs(tableEditLogExample,offset,limit);

    }

}
